<template>
    <div class="chat-input">
        <el-scrollbar v-if="isEmojiPickerVisible" class="emoji-picker">
            <div v-for="emoji in emojiList" :key="emoji" class="emoji-item"
                @click="insertEmoji(emoji.emoji)">
                {{ emoji.emoji }}
            </div>
        </el-scrollbar>
        <div class="textarea-container">
            <el-input type="textarea" v-model="newMessage" placeholder="发送消息..." @keyup.enter.exact="sendMessage($event)"
                @keyup.ctrl.enter="addNewLine" ref="chatInput" :rows="3" @blur="blurEvent" class="no-border-textarea">
            </el-input>
        </div>
        <div class="action-buttons">
            <el-button @click="toggleEmojiPicker" class="emoji-button">
                <img src="@/assets/emoji.png" alt="Emoji" class="emoji-icon" />
            </el-button>
            <el-button type="primary" @click="sendMessage" class="send-button">
                发送
            </el-button>
        </div>
    </div>
</template>

<script setup>
    import { ref, defineEmits } from 'vue';
    import { emojiList } from "../../assets/emoji.js";

    const emit = defineEmits(['send'])

    // 是否显示表情选择器
    const isEmojiPickerVisible = ref(false);

    // 
    const newMessage = ref("");

    // 输入框光标位置
    const inputSelectionStart = ref(-1);
    var sel;
    const chatInput = ref();
    const blurEvent = (val) => {
        sel = window.getSelection();
        inputSelectionStart.value = val.srcElement.selectionStart;

        console.log(inputSelectionStart.value);
    };

    // 处理 Emoji 按钮点击事件
    const toggleEmojiPicker = (event) => {
        if (isEmojiPickerVisible.value) {
            isEmojiPickerVisible.value = false;
        } else {
            isEmojiPickerVisible.value = true;
        }
    };

    // 处理 Emoji 选择事件
    const insertEmoji = (emoji) => {
        const inputElement = document.querySelector(".el-textarea__inner");
        if (inputElement) {
            if (inputSelectionStart.value == -1) {
                return (newMessage.value += emoji);
            }
            let index = inputSelectionStart.value;
            let str = newMessage.value;
            newMessage.value = str.slice(0, index) + emoji + str.slice(index);
            inputSelectionStart.value = -1;
        }

        isEmojiPickerVisible.value = false;
    };

    // 发送消息的处理
    const sendMessage = (event) => {
        if (event) {
            event.preventDefault(); // 阻止默认换行行为
        }
        if (!newMessage.value.trim()) return;

        // 去除换行符
        const messageData = {
            type: "chat",
            msg: newMessage.value.trim().replace(/\n/g, ''),
        };

        emit('send', messageData);

        newMessage.value = "";
    };

    // 添加换行处理
    const addNewLine = () => {
        newMessage.value += '\n';
    };
</script>

<style lang="less" scoped>
    .chat-input {
        padding: 15px 20px;
        border-top: 1px solid #eee;
        background: #fff;
        border-radius: 0 0 12px 12px;
        position: relative;
        display: flex;
        flex-direction: column;
        align-items: flex-end; // 确保表情按钮在右侧
    }

    .emoji-picker {
        position: absolute;
        background: #fff;
        border: 1px solid #ccc;
        border-radius: 4px;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
        z-index: 1000;
        padding: 5px;
        display: flex;
        flex-wrap: wrap;
        width: 400px;
        animation: fadeInUp 0.3s ease-in-out;
        top: -210px; // 将 emoji-picker 放在 chat-input 上方
        left: 0;
        right: 0;
        margin-left: auto;
        margin-right: auto;
    }

    .emoji-picker {
        height: 300px;
        position: absolute;
        background: #fff;
        border: 1px solid #ccc;
        border-radius: 4px;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
        z-index: 1000;
        padding: 5px;

        width: 400px;
        animation: fadeInUp 0.3s ease-in-out;

        :deep(.el-scrollbar__view) {
            display: flex;
            flex-wrap: wrap;
        }

        .emoji-item {
            width: 38px;
            height: 38px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            margin: 2px;
            font-size: 24px;

            &:hover {
                background: #f0f0f0;
            }
        }
    }


    .textarea-container {
        position: relative;
        width: 100%;
        display: flex;
        flex-direction: column;
        align-items: flex-end; // 确保发送按钮在右侧
    }

    .el-textarea {
        width: 100%;
        margin-bottom: 10px; // 添加间距
    }

    .no-border-textarea :deep(.el-textarea__inner) {
        box-shadow: none; // 去掉边框
        resize: none; // 禁止调整大小
        &:focus {
            border: none;
            outline: none;
        }
    }

    .action-buttons {
        display: flex;
        justify-content: space-between;
        width: 100%;
        align-items: center;
    }

    .send-button {
        margin-left: auto;
    }

    .emoji-button {
        background: none;
        border: none;
        cursor: pointer;
    }

    .emoji-icon {
        width: 24px;
        height: 24px;
    }

    @keyframes fadeInUp {
        from {
            opacity: 0;
            transform: translateY(-1px);
        }

        to {
            opacity: 1;
            transform: translateY(0px);
        }
    }
</style>